package com.cenntro.oss.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cenntro.common.base.BaseController;
import com.cenntro.common.util.ConvertorUtils;
import com.cenntro.common.util.result.Result;
import com.cenntro.oss.model.dbo.DevDevicePartsDO;
import com.cenntro.oss.model.qo.DevDevicePartsQO;
import com.cenntro.oss.service.DevDevicePartsService;
import com.google.common.collect.ImmutableMap;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 零部件信息
 * </p>
 *
 * @author
 * @since 2018-12-08
 */

@Api(value = "零部件", tags = { "零部件信息" })
@RestController
@RequestMapping("/deviceParts")
public class DevDevicePartsController extends BaseController {

	@Resource
	private DevDevicePartsService devDevicePartsService;

	/**
	 * 
	* @Title addOrUpdate
	* @Description 录入或者修改配件信息
	* @param qo
	* @return  
	* @throws
	 */
	@ApiOperation(value = "零部件新增或修改", notes = "零部件")
	@RequestMapping("/addOrUpdate")
	public Result addOrUpdate(DevDevicePartsQO qo) {
        DevDevicePartsDO ddpd = ConvertorUtils.deepConvert(qo, DevDevicePartsDO.class);
        if (ddpd.getRefId() != null && ddpd.getRefId() > 0) {  //修改
			devDevicePartsService.update(ddpd);
		}else{  	//新增
			devDevicePartsService.insert(ddpd);
		}
		return Result.ok();
	}
	
	/**
	 * 
	* @Title delete
	* @Description 零部件删除
	* @param refId
	* @return  
	* @throws
	 */
	@RequestMapping("/delete")
	public Result delete(Integer refId) {
        if (refId == null || refId < 0) {
			return Result.error("参数不合法");
		}
		DevDevicePartsDO ddpd = devDevicePartsService.selectById(refId);
		if(ddpd.getRefId()==null){
			return Result.error("无此id信息");
		}
		devDevicePartsService.deleteById(refId);
		return Result.ok();
		
	}
	
	/**
	 * 
	* @Title list
	* @Description 零部件列表
	* @param devId
	* @return  
	* @throws
	 */
	@RequestMapping("/list")
	public Result list(Integer devId) {
        if (devId == null || devId < 0) {
			return Result.error("参数不合法");
		}
		List<DevDevicePartsDO> list = devDevicePartsService.selectByMap(ImmutableMap.of("devId",devId));
        if (list == null) {
			return Result.error("无此id信息");
		}
		return Result.ok(list);
		
	}

}
